<?php
namespace app\entries\model;
use think\Model;



// +************
// | 词条管理
// +************
class Entries extends Model
{
    /**************************匠人部分**************************/
    //显示匠人信息
    function showjr($page,$limit)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//产区id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组

        $res = db("jiangren")->limit($limit)->page($page)->select();
        foreach ($res as &$oneres) {
            $oneres["bangdan"] = $bangdan_ids[$oneres["bangdan_id"]];
            $oneres["teacase"] = $teacase_ids[$oneres["teacase_id"]];
            $oneres["teaarea"] = $teaarea_ids[$oneres["teaarea_id"]];
        }

        return $res;
    }


    //添加匠人数据（单个）
    function addjr($data)
    {
        $insdata = $this->insertData($data);
        db('jiangren')->insert($insdata);
        return 1;
    }


    //上传的匠人的Excel数据存入数据库
    function insertExcel($data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组

        //Excel数据插入数据表
        foreach ($data as $onedata) {
            $insertdata =[];

            //判断茶产地是否在已经备案
            if(!array_key_exists($onedata["teaarea"], $teaarea_ids)){
                db("teaarea")->insert(["area"=>$onedata["teaarea"]]);
                $newId = db("teaarea")->getLastInsID();
                $teaarea_ids[$onedata["teaarea"]] = $newId;
                //关联茶类和茶产地
                $insCaseArea['teacase_id'] = $teacase_ids[$onedata["teacase"]];
                $insCaseArea['teaarea_id'] = $newId;
                db('case_area')->insert($insCaseArea);
                $teaarea_ids[$data["teaarea_id"]] = $newId;
            }

            $insertdata =[
                "teacase_id" => $teacase_ids[$onedata['teacase']],
                "teaarea_id" => $teaarea_ids[$onedata['teaarea']],
                "bangdan_id" => $bangdan_ids[$onedata['bangdan']],
                "name" =>$onedata['name'],
                "score" =>$onedata['score'],
                "briefintro" =>$onedata['briefintro'],
                "contact" =>$onedata['contact'],
                "phone" =>$onedata['phone'],
                "wechat" =>$onedata['wechat'],
                "detailintro" =>$onedata['detailintro'],
                "addtime" => time()
            ];
            db("jiangren")->insert($insertdata);
        }

        return 1;
    }

    //显示需要修改匠人信息
    function modifyjr($id)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//茶类id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组
        $oneres = db("jiangren")->find($id);
        $oneres["bangdan_id"] = $bangdan_ids[$oneres["bangdan_id"]];
        $oneres["teacase_id"] = $teacase_ids[$oneres["teacase_id"]];
        $oneres["teaarea_id"] = $teaarea_ids[$oneres["teaarea_id"]];
        return $oneres;

    }

    //修改匠人信息
    function updatejr($id, $data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组
        $updata = $data;
        $updata["teacase_id"] = $teacase_ids[$data["teacase_id"]];
        $updata["teaarea_id"] = $teaarea_ids[$data["teaarea_id"]];
        $updata["bangdan_id"] = $bangdan_ids[$data["bangdan_id"]];
        db('jiangren')->where("id",$id)->update($updata);
        return 1;
    }

    //删除匠人信息
    function deljr($id)
    {
        db("jiangren")->where("id",$id)->delete();
        return 1;
    }



    /**************************品牌部分**************************/


    // 显示品牌信息
    function showpp($page,$limit)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//茶类id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组

        $res = db("pinpai")->limit($limit)->page($page)->select();
        foreach ($res as &$oneres) {

            $oneres["bangdan"] = $bangdan_ids[$oneres["bangdan_id"]];
            $oneres["teacase"] = $teacase_ids[$oneres["teacase_id"]];
            $oneres["teaarea"] = $teaarea_ids[$oneres["teaarea_id"]];
        }

        return $res;
    }

    //添加品牌数据（单个）
    function addpp($data)
    {
        $insdata = $this->insertData($data);
        db('pinpai')->insert($insdata);
        return 1;
    }

    //显示修改品牌数据
    function modifypp($id)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//茶类id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组
        $oneres = db("pinpai")->find($id);
        $oneres["bangdan_id"] = $bangdan_ids[$oneres["bangdan_id"]];
        $oneres["teacase_id"] = $teacase_ids[$oneres["teacase_id"]];
        $oneres["teaarea_id"] = $teaarea_ids[$oneres["teaarea_id"]];
        return $oneres;
    }

    //修改品牌数据
    function updatepp($id, $data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组
        $updata = $data;
        $updata["teacase_id"] = $teacase_ids[$data["teacase_id"]];
        $updata["teaarea_id"] = $teaarea_ids[$data["teaarea_id"]];
        $updata["bangdan_id"] = $bangdan_ids[$data["bangdan_id"]];
        db('pinpai')->where("id",$id)->update($updata);
        return 1;
    }

    //删除品牌信息
    function delpp($id)
    {
        db("pinpai")->where("id",$id)->delete();
        return 1;
    }


    //将Excel里的品牌信息导入数据库
    function insertExcelpp($data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组

        //Excel数据插入数据表
        foreach ($data as $onedata) {
            $insertdata =[];

            //判断茶产地是否在已经备案
            if(!array_key_exists($onedata["teaarea"], $teaarea_ids)){
                db("teaarea")->insert(["area"=>$onedata["teaarea"]]);
                $newId = db("teaarea")->getLastInsID();
                $teaarea_ids[$onedata["teaarea"]] = $newId;
                //关联茶类和茶产地
                $insCaseArea['teacase_id'] = $teacase_ids[$onedata["teacase"]];
                $insCaseArea['teaarea_id'] = $newId;
                db('case_area')->insert($insCaseArea);
                $teaarea_ids[$data["teaarea_id"]] = $newId;
            }

            $insertdata =[
                "teacase_id" => $teacase_ids[$onedata['teacase']],
                "teaarea_id" => $teaarea_ids[$onedata['teaarea']],
                "bangdan_id" => $bangdan_ids[$onedata['bangdan']],
                "name" =>$onedata['name'],
                "relatedwords" =>$onedata['relatedwords'],
                "sale" =>$onedata['sale'],
                "company" =>$onedata['company'],
                "pnumber" =>$onedata['pnumber'],
                "contact" =>$onedata['contact'],
                "phone" =>$onedata['phone'],
                "wechat" =>$onedata['wechat'],
                "detailintro" =>$onedata['detailintro'],
                "addtime" => time()
            ];
            db("pinpai")->insert($insertdata);
        }

        return 1;
    }


    /**************************珍品部分**************************/

    // 显示珍品信息
    function showzp($page,$limit)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//茶类id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组

        $res = db("zhenpin")->limit($limit)->page($page)->select();
        foreach ($res as &$oneres) {

            $oneres["bangdan"] = $bangdan_ids[$oneres["bangdan_id"]];
            $oneres["teacase"] = $teacase_ids[$oneres["teacase_id"]];
            $oneres["teaarea"] = $teaarea_ids[$oneres["teaarea_id"]];
        }
        return $res;
    }

    //手动增加珍品信息
    function addzp($data)
    {
        $insdata = $this->insertData($data);
        db('zhenpin')->insert($insdata);
        return 1;
    }

    //显示修改的珍品信息
    function modifyzp($id)
    {
        $teacase_ids = $this->teacase_ids();//茶类id和名称的关联数组
        $teaarea_ids = $this->teaarea_ids();//茶类id和产区的关联数组
        $bangdan_ids = $this->bangdan_ids();//榜单id和名称的关联数组
        $oneres = db("zhenpin")->find($id);
        $oneres["bangdan_id"] = $bangdan_ids[$oneres["bangdan_id"]];
        $oneres["teacase_id"] = $teacase_ids[$oneres["teacase_id"]];
        $oneres["teaarea_id"] = $teaarea_ids[$oneres["teaarea_id"]];
        return $oneres;
    }

    //修改珍品数据
    function updatezp($id, $data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组
        $updata = $data;
        $updata["teacase_id"] = $teacase_ids[$data["teacase_id"]];
        $updata["teaarea_id"] = $teaarea_ids[$data["teaarea_id"]];
        $updata["bangdan_id"] = $bangdan_ids[$data["bangdan_id"]];
        db('zhenpin')->where("id",$id)->update($updata);
        return 1;
    }

    //删除珍品信息
    function delzp($id)
    {
        db("zhenpin")->where("id",$id)->delete();
        return 1;
    }


    //将Excel里的珍品信息导入数据库
    function insertExcelzp($data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组

        //Excel数据插入数据表
        foreach ($data as $onedata) {
            $insertdata =[];

            //判断茶产地是否在已经备案
            if(!array_key_exists($onedata["teaarea"], $teaarea_ids)){
                db("teaarea")->insert(["area"=>$onedata["teaarea"]]);
                $newId = db("teaarea")->getLastInsID();
                $teaarea_ids[$onedata["teaarea"]] = $newId;
                //关联茶类和茶产地
                $insCaseArea['teacase_id'] = $teacase_ids[$onedata["teacase"]];
                $insCaseArea['teaarea_id'] = $newId;
                db('case_area')->insert($insCaseArea);
                $teaarea_ids[$data["teaarea_id"]] = $newId;
            }

            $insertdata =[
                "teacase_id" => $teacase_ids[$onedata['teacase']],
                "teaarea_id" => $teaarea_ids[$onedata['teaarea']],
                "bangdan_id" => $bangdan_ids[$onedata['bangdan']],
                "name" => $onedata['name'],
                "producer" => $onedata['producer'],
                "zs" => $onedata['zs'],
                "gs" => $onedata['gs'],
                "ct" => $onedata['ct'],
                "ds" => $onedata['ds'],
                "other" => $onedata['other'],
                "price" => $onedata['price'],
                "contact" => $onedata['contact'],
                "phone" => $onedata['phone'],
                "wechat" => $onedata['wechat'],
                "detailintro" => $onedata['detailintro'],
                "addtime" => time()
            ];
            db("zhenpin")->insert($insertdata);
        }

        return 1;
    }





    // +************************
    // | 以下为类内部调用函数
    // +************************

    //茶类id和名称的关联数组,如[1=>红茶]
    function teacase_ids()
    {
        $teacase_ids = [];//茶类id和名称的关联数组
        $res = db('teacase')->select();
        foreach ($res as $key => $value) {
            $teacase_ids[$value["id"]] = $value["teaname"];
        }
        return $teacase_ids;
    }

    //返回茶类id和产区的关联数组,如[1=>武夷]
    function teaarea_ids()
    {
        $teaarea_ids = [];
        $res = db('teaarea')->select();
        foreach ($res as $key => $value) {
            $teaarea_ids[$value["id"]] = $value["area"];
        }
        return $teaarea_ids;
    }

    //返回榜单id和名称的关联数组,如[1=>匠人]
    function bangdan_ids()
    {
        $bangdan_ids = [];//榜单id和名称的关联数组
        $res = db('bangdan')->select();
        foreach ($res as $key => $value) {
            $bangdan_ids[$value["id"]] = $value["bdname"];
        }
        return $bangdan_ids;
    }

    //返回茶类名称和id的关联数组,如[红茶=>1]
    function ids_teacase()
    {
        $teacase_ids = [];//茶类名称和id的关联数组
        $res = db('teacase')->select();
        foreach ($res as $key => $value) {
            $teacase_ids[$value["teaname"]] = $value["id"];
        }
        return $teacase_ids;
    }

    //返回茶类产区和id的关联数组,如[武夷=>1]
    function ids_teaarea()
    {
        $teaarea_ids = [];//茶类产区和id的关联数组
        $res = db('teaarea')->select();
        foreach ($res as $key => $value) {
            $teaarea_ids[$value["area"]] = $value["id"];
        }
        return $teaarea_ids;
    }

    //返回榜单名称和id的关联数组,如[匠人=>1]
    function ids_bangdan()
    {
        $bangdan_ids = [];//榜单名称和id的关联数组
        $res = db('bangdan')->select();
        foreach ($res as $key => $value) {
            $bangdan_ids[$value["bdname"]] = $value["id"];
        }
        return $bangdan_ids;
    }


    //插入数据
    function insertData($data)
    {
        $teacase_ids = $this->ids_teacase();//茶类名称和id的关联数组
        $teaarea_ids = $this->ids_teaarea();//茶类产区和id的关联数组
        $bangdan_ids = $this->ids_bangdan();//榜单名称和id的关联数组
        $insdata = $data;
        $insdata["teacase_id"] = $teacase_ids[$data["teacase_id"]];
        $insdata["bangdan_id"] = $bangdan_ids[$data["bangdan_id"]];

        //判断茶产地是否在已经备案
        if(array_key_exists($data["teaarea_id"], $teaarea_ids)){
            $insdata["teaarea_id"] = $teaarea_ids[$data["teaarea_id"]];
        }else{
            db("teaarea")->insert(["area"=>$data["teaarea_id"]]);
            $newId = db("teaarea")->getLastInsID();
            $insdata["teaarea_id"] = $newId;

            //关联茶类和茶产地
            $insCaseArea['teacase_id'] = $teacase_ids[$data["teacase_id"]];
            $insCaseArea['teaarea_id'] = $newId;
            db('case_area')->insert($insCaseArea);
            $teaarea_ids[$data["teaarea_id"]] = $newId;
        }
        return $insdata;
    }

    //返回$table表数据总数量
    function number($table)
    {
        $res = db($table)->select();
        $num = count($res);
        return $num;
    }




}

?>